package com.wanmait.gohome.controller.manage;

import com.github.pagehelper.PageInfo;
import com.wanmait.gohome.pojo.Area;
import com.wanmait.gohome.pojo.Userinfo;
import com.wanmait.gohome.service.AreaService;
import com.wanmait.gohome.service.UserinfoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/manage")
public class UserController {

    @Resource
    private UserinfoService userinfoService;
    @Resource
    private AreaService areaService;

    @RequestMapping("/show/userinfoshow")
    public ModelAndView finduserinfo()
    {
        List<Area> areas = areaService.findAll();
        HashMap<Integer,Area> typeMap = new HashMap<Integer,Area>();
        for(Area area:areas)
        {
            typeMap.put(area.getAreaId(), area);
        }
        List<Area> root= new ArrayList<>();//装省
        for(Area area:areas)
        {
            if(area.getParentId()==-1)
            {
                root.add(area);
            }
            else
            {
                Area parent = typeMap.get(area.getParentId());
                List<Area> areas1 = parent.getAreaList();
                if(areas1==null) {
                    areas1 = new ArrayList<Area>();
                    parent.setAreaList(areas1);
                }
                areas1.add(area);
            }

        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("root",root);
        return  mv;

    }
    @RequestMapping("/ajaxJsp/sethonor")
    @ResponseBody
    public ModelAndView sethonor()
    {
        PageInfo<Userinfo> pageInfo  =  userinfoService.findPage1();
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo",pageInfo);
        return mv;
    }
    //显示userinfoshow页面
    @RequestMapping("/ajaxJsp/usershow")
    @ResponseBody
    public ModelAndView usershow()
    {
        PageInfo<Userinfo> pageInfo  =  userinfoService.findPage();
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo",pageInfo);
        return mv;

    }
    //加条件显示userinfoshow页面
    @RequestMapping("/ajaxJsp/usershow1")
    public ModelAndView usershow1(String name,Integer province,Integer city,Integer area)
    {
        PageInfo<Userinfo> pageInfo  =  userinfoService.conditionQuery(name,province,city,area);

        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo",pageInfo);
        mv.addObject("province",province);
        mv.addObject("name",name);
        mv.addObject("city",city);
        mv.addObject("area",area);
        return mv;

    }
    @RequestMapping("/show/closure")//封停账号
    @ResponseBody
    public void closure(Userinfo userinfo)
    {
        userinfoService.updateEnable(userinfo);
    }
    @RequestMapping("/show/deblocking")//解封
    @ResponseBody
    public void deblocking(Userinfo userinfo)
    {

            userinfoService.updateEnable(userinfo);
    }
    @RequestMapping("/ajaxJsp/userdetails")
    public ModelAndView userdetails(String id)
    {

        Userinfo userinfo  = userinfoService.findUserdetails(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("userinfo",userinfo);

        return mv;
    }


    //志愿者界面
    @GetMapping("volunteer/show")
    public void volunteer(){}
    @PostMapping("ajaxJsp/volunteerShow")
    @ResponseBody
    public ModelAndView volunteerShow(){
        PageInfo<Userinfo> volunteerPage = userinfoService.selectVolunteers();
        ModelAndView mv = new ModelAndView();
        mv.addObject("volunteerPage",volunteerPage);
        mv.addObject("counts",userinfoService.getVolunteerApplyCount());
        mv.setViewName("manage/ajaxJsp/volunteerShow");
        return mv;
    }



    //志愿者删除
    @PostMapping("volunteer/delete")
    @ResponseBody
    public void delete(Integer id){
        userinfoService.deleteVolunteer(id);
    }
//查询志愿者信息
    @GetMapping("ajaxJsp/volunteerShowInfo")
    @ResponseBody
    public ModelAndView findById(String id){
       ModelAndView mv = new ModelAndView();
       Userinfo volunteer = userinfoService.findVolunteerById(Integer.parseInt(id));
       mv.addObject("volunteer",volunteer);
       return mv;
    }
    //恢复志愿者
    @PostMapping("recover")
    @ResponseBody
    public void recover(Integer id){
        userinfoService.recover(id);
    }
    //模糊查询
    @PostMapping("ajaxJsp/SeachVolunteer")
    @ResponseBody
    public ModelAndView seach(String title){
        ModelAndView mv = new ModelAndView();
       PageInfo<Userinfo> seachVolunteer =  userinfoService.volunteerQuery(title);
       mv.addObject("title",title);
        mv.addObject("seachVolunteer",seachVolunteer);
        return mv;
    }
    //志愿者审核列表
    @RequestMapping("ajaxJsp/volunteerApply")
    public ModelAndView volunteerApply(){
        PageInfo<Userinfo> pageInfo = userinfoService.selectVolunteerApply();
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo",pageInfo);
        return mv;
    }
    //志愿者通过
    @PostMapping("VolunteerPass")
    @ResponseBody()
    public void pass(Integer id){
        userinfoService.VolunteerPass(id);
    }
    //志愿者申请驳回
    @PostMapping("volunteerReject")
    @ResponseBody()
    public void reject(Integer id){
        userinfoService.VolunteerReject(id);
    }
    //志愿者申请驳回理由
    @PostMapping("deleteReason")
    @ResponseBody()
    public void insertReason(Userinfo userinfo){
        userinfoService.insertReject(userinfo);
    }
    @GetMapping("ajaxJsp/volunteerApplyInfo")
    @ResponseBody
    public ModelAndView info(Integer id){
        ModelAndView mv = new ModelAndView();
        Userinfo volunteerApplyInfo = userinfoService.volunteerApplyInfo(id);
        mv.addObject("volunteerApplyInfo",volunteerApplyInfo);
        return mv;
    }
}
